package com.content.view.Component.OnlineRes
{
	import com.clerkq.CircleList.CircleListH;
	import com.clerkq.CircleList.data.ThumbHVO;
	import com.clerkq.CircleList.view.ThumbH;
	import com.content.model.Collection.ItemGroup;
	import com.content.model.LoadedAssets.SkinProxy;
	import com.content.model.OnlineRes.OnlineResVO;
	import com.content.view.Component.Button.ImageButton;
	import com.content.view.Frame.MainPanel.SPBase;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import org.aswing.JOptionPane;
	
	public class SPCart extends SPBase
	{
		public static const PLAY_RES:String = "SPCart-PlayRes";
		
		private var _itemGroup:ItemGroup;
		//private var _resWrapper:Sprite;
//		private var _insertX:int;
		
		private var _clContainer:Sprite;
		private var _cl:CircleListH;
		private var _clBtnLeft:ImageButton;
		private var _clBtnRight:ImageButton;
		private var _shelfObj:Sprite;
		private var _clRect:Rectangle;
		
		public function SPCart()
		{
			super(this);
//			_itemGroup = new ItemGroup();
			_clContainer = new Sprite();
			
			_clBtnLeft = new ImageButton(new SkinProxy.IMG_BookShelf_ArrowLeft() as Sprite);
			_clBtnRight = new ImageButton(new SkinProxy.IMG_BookShelf_ArrowRight() as Sprite);
			_clBtnLeft.id = "CL_Left";
			_clBtnRight.id = "CL_Right";
			
			_clRect = new Rectangle();
			_clRect.left = 5; _clRect.right = 5; _clRect.top = 5; _clRect.bottom = 5;
			_cl = new CircleListH(6, 130, 120, 175, _clRect);
			
			_shelfObj = new SkinProxy.IMG_BookShelf_BG() as Sprite;
			
			addChild(_clContainer);
			_clContainer.addChild(_shelfObj);
			_clContainer.addChild(_cl);
			_clContainer.addChild(_clBtnLeft);
			_clContainer.addChild(_clBtnRight);
			
			addEventListener(MouseEvent.CLICK, OnClick);
		}
		
		private function RemoveAllItem():void{
			if(_itemGroup != null)_itemGroup.Clear();
		}
		
		/* Event Handler
		 * ------------------------------------------------------- */
		private function OnClick(evt:MouseEvent):void{
			if(evt.target is ThumbH){
				var rItem:ThumbH = evt.target as ThumbH;
				dispatchEvent(new OnlineResEvent(PLAY_RES, false, false, rItem.vo));
			}else if(evt.target is ImageButton){
				var imgBtn:ImageButton = evt.target as ImageButton;
				switch(imgBtn.id){
					case "CL_Left":
						_cl.GoPrevious();
						break;
					case "CL_Right":
						_cl.GoNext();
						break;
				}
			}
		}
		
		private function Included(resVO:OnlineResVO):Boolean{
			var itemVO:ThumbHVO;
			for(var i:uint = 0; i < _itemGroup.Length; i++){
				itemVO = _itemGroup.Items[i] as ThumbHVO;
				if(itemVO.ResID == resVO.ResID){
					return true;
				}
			}
			return false;
		}
		
		public function AddToCart(resVO:OnlineResVO):void{
			if(Included(resVO)){
				//MsgBox.instance.Show("对不起，该资源已经添加，您不能重复购买同一份资源。", MsgBoxType.OK, null);
				var jop:JOptionPane = JOptionPane.showMessageDialog(
					"提示",
					"对不起，该资源已经添加，您不能重复购买同一份资源。",null,	null, true, null,JOptionPane.OK);
				jop.getFrame().setDefaultButton(jop.getYesButton());
			}else{
				var newVO:ThumbHVO = new ThumbHVO();
				newVO.ResID = resVO.ResID;
				newVO.Name = resVO.Name;
				newVO.Description = resVO.Author;
				newVO.Publisher = resVO.Publisher;
				newVO.PublishDate = resVO.PublishDate;
				newVO.Star = resVO.Star;
				newVO.Purchased = resVO.Purchased;
				newVO.CoverURL = resVO.CoverURL;
				newVO.CoverURLSmall = resVO.CoverURLSmall;
				newVO.DownUrl = resVO.DownUrl;
				
				if(_itemGroup.Length == 0){
					newVO.Index = 0;
				}else{
					newVO.Index = ThumbHVO(_itemGroup.Items[_itemGroup.Length-1]).Index + 1;
				}
				_itemGroup.AddVO(newVO);
				_cl.FillList(_itemGroup);
				SetLayout(_areaW, _areaH);	// 得到数据后，手动重排内容
			}			
		}
		
		public function FillResWrapper(itemGroup:ItemGroup):void{
			RemoveAllItem();
			_itemGroup = itemGroup;
			_cl.FillList(itemGroup);
			SetLayout(_areaW, _areaH);	// 得到数据后，手动重排内容
		}
		
		override public function SetLayout(areaWidth:int, areaHeight:int):void{
			super.SetLayout(areaWidth, areaHeight);
			
			_shelfObj.y = 140;
			_clBtnLeft.x = 20;
			_clBtnLeft.y = _clBtnRight.y = 60;
			_cl.x = int(_clBtnLeft.x + _clBtnLeft.width);			
			_clBtnRight.x = int(_cl.x + _cl.width);
			
			_clContainer.x= int((areaWidth - _shelfObj.width - 8)/2);
			_clContainer.y = int((areaHeight - _clContainer.height)/2);
			
//			_clContainer.graphics.clear();
//			_clContainer.graphics.beginFill(0xff0000, 0.3);
//			_clContainer.graphics.drawRect(0,0,_clContainer.width, _clContainer.height);
		}
		
		override public function Dispose():void{
			super.Dispose();
			removeEventListener(MouseEvent.CLICK, OnClick);
		}
	}
}